﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using G.SL.Controls;
using System.Collections.ObjectModel;
using System.IO;
using G.APITest.Utils;

namespace G.APITest.Views
{
    public partial class ExtendPage : Page
    {
     
    
        public ExtendPage()
        {
            InitializeComponent();
            MenuItem item1 = new MenuItem() { Name = "视图" };
            foreach (var item in ExtendPageUtiles.AppPluginInfos)
            {
                item1.Items.Add(new MenuItem() { Name = item.Name, Command = new SL.MVVM.CallBackCommand(OpenView), CommandParamter = item });
            }
            item1.Items.Add(new MenuItem() { Name = "添加插件", HasSplitter = true, Command = new SL.MVVM.CallBackCommand(OpenView) });
            ExtendPageUtiles.AppPluginInfos.CollectionChanged += (s, e) =>
            {
                if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                {
                    foreach (Data.APPPluginInfo item in e.NewItems)
                    {
                        item1.Items.Add(new MenuItem() { Name = item.Name, Command = new SL.MVVM.CallBackCommand(OpenView), CommandParamter = item });
                    }
                }
            };
            PageMenuService.GetMenu(this).Add(item1);
        }

        //private void AddPlugin(object p)
        //{
        //    ChildWindows.AddAPPPluginChildWindow w = null;
        //    w = new ChildWindows.AddAPPPluginChildWindow((buffer, url) =>
        //   {
        //       string name = System.IO.Path.GetFileNameWithoutExtension(url);
        //       if (AppPluginInfos.FirstOrDefault(c => c.Name == w.PlugIn.Name) != null)
        //       {
        //           MessageBox.Show("已经存在该插件");
        //           return;
        //       }
        //       //保存文件
        //       using (var isofile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
        //       {
        //           using (var dllFile = isofile.CreateFile(DirectoryName + "/" + name))
        //           {
        //               dllFile.Write(buffer, 0, buffer.Length);
        //               dllFile.Close();
        //           }
        //           AppPluginInfos.Add(new Data.APPPluginInfo() { Name = w.PlugIn.Name, ISOPath = DirectoryName + "/" + name, DLLName = name, Uri = url });
        //           SaveAppPluginInfos();
        //       }
        //       w.Close();
        //   });
        //    w.Show();
        //}
        private void OpenView(object p)
        {
            Data.APPPluginInfo info = p as Data.APPPluginInfo;
            Telerik.Windows.Controls.RadPane pane = docking.GetVisualDescendants<Telerik.Windows.Controls.RadPane>().FirstOrDefault(c => c.DataContext == p);
            if (pane == null)
            {
                if (info == null)
                {
                    pane = new Telerik.Windows.Controls.RadPane() { Header = "添加插件" };
                    pane.Content = new Controls.EditePluginControl();
                }
                else if (info != null && !info.IsOpen)
                {
                    Utils.MEFUtils.LoadPart(info.ISOPath, info);
                    if (info.PlugIn == null)
                    {
                        MessageBox.Show("插件无效或者已过期，请更新插件");
                        return;
                    }
                    //info.IsOpen = true;
                    pane = new Telerik.Windows.Controls.RadPane() { Header = info.DLLName };
                    pane.Content = info.PlugIn.CreateContent();
                }
                if (pane != null)
                {
                    pane.DataContext = p;
                    paneGroup.AddItem(pane, Telerik.Windows.Controls.Docking.DockPosition.Center);
                }
            }
            else
            {
                pane.IsSelected = true;
            }
        }
        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

    }
}
